package com.feng.d18Segregation;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 定时任务调度类
 * @author LiYilin on 2020/4/13
 */
public class ScheduledUpdater {

    /**
     * 手动创建线程池，效果会更好哦。
     */
    private final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
    private int initialDelayInSeconds;
    private long periodInSeconds;
    private Update update;

    public ScheduledUpdater(int initialDelayInSeconds, long periodInSeconds, Update update) {
        this.initialDelayInSeconds = initialDelayInSeconds;
        this.periodInSeconds = periodInSeconds;
        this.update = update;
    }

    public void execute() {
        service.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                update.update();
            }
        }, initialDelayInSeconds, periodInSeconds, TimeUnit.SECONDS);
    }
}
